package com.genesis.yunnanji.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.genesis.yunnanji.R;
import com.genesis.yunnanji.adapter.HelpAdapter;
import com.genesis.yunnanji.bean.HelpBean;
import com.genesis.yunnanji.config.GenesisApiConfig;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;


public class Help extends BaseActivity {
    @ViewInject(R.id.tb_help_toolbar)
    private Toolbar mToolbar;

    @ViewInject(R.id.rv_info)
    private RecyclerView rvInfo;

    private HelpAdapter mHelpAdapter;

    private List<HelpBean.ResultBean.ListBean> mListData = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState, R.layout.activity_help);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mToolbar.setTitle("");
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void setUpView() {
        initNet();
    }

    private void initNet() {
        OkHttpUtils.get()
                .url(GenesisApiConfig.HOST + GenesisApiConfig.HELP_CENTER)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Gson gson = new Gson();
                        final HelpBean bean = gson.fromJson(response, HelpBean.class);
                        if (bean.status == 200) {
                            mListData = bean.result.list;
                            rvInfo.setLayoutManager(new LinearLayoutManager(context));
                            mHelpAdapter = new HelpAdapter(Help.this, mListData);
                            rvInfo.setAdapter(mHelpAdapter);
                            mHelpAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                                @Override
                                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                    Intent intent = new Intent(Help.this, WebActivity.class);
                                    intent.putExtra("url", mListData.get(position).taget_link);
                                    intent.putExtra("news_id", "");
                                    intent.putExtra("title",mListData.get(position).title);
                                    startActivity(intent);
                                }
                            });
                        }
                    }
                });
    }
}
